<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Design by Free CSS Templates
http://www.freecsstemplates.org
Released for free under a Creative Commons Attribution 2.5 License

Name       : Everyday Series
Description: A three-column, fixed-width blog design.
Version    : 1.0
Released   : 20090617

-->
<?php 
	session_start();
	require_once('functions.php');
	require_once('LeaveFormView.php');	
	
	$flag = "";
	$errorFlag = 0;
	$viewLeaveFlag = 0;
	
	if(isset($_POST['searchForEmpNum'])){
		$link = makeSQLconnection();
		$employeeNumber = $_POST['employeeNumber'];
		$result = checkEmpNo($employeeNumber);
		if(!$result){
			$errorFlag = 1;
		}else{
			$query = sprintf("SELECT * FROM `EMPLOYEE` WHERE 
							  `Employee_number` = '$employeeNumber'");
			$return = mysql_query($query, $link);
			
			if (!$return) {
				$success = 3;
			} else {
				$success = 1;
			}
			
			$found = false;
			if($success == 1) {
				while($data = mysql_fetch_assoc($return)){
					if($data['Employee_number'] == $employeeNumber) {
						$_SESSION['employeeNumber'] = $employeeNumber;						
						$_SESSION['gender'] = $data['Employee_gender'];
						header('Location: menu.php');
					}
				}
			}
			
			if($found == false)
				$errorFlag=2;
			
			mysql_close($link);
			
		}
	}
	

	//SEARCH FOR NAME
	$checkFirstName = true;
	$checkMidName = true;
	$checkLastName = true;
	$moreThanOneFlag = false;

	if(isset($_POST['searchForName'])){
		$link = makeSQLconnection();
		$empNo = "";
		$failed = false;
		
		$firstName = $_POST['empFirstName'];
		$midName = $_POST['empMidName'];
		$lastName = $_POST['empLastName'];
		
		$checkFirstName = nameValidator($firstName);
		$checkMidName = nameValidator($midName);
		$checkLastName = nameValidator($lastName);
	
		if($checkFirstName && $checkMidName && $checkLastName){
			$errorFlag2 = false;
			$return = LeaveManager :: getLeaveDataName($_POST['empFirstName'], $_POST['empMidName'], $_POST['empLastName']);
			$employeeNumber = "";
			
			if (!$return) {
				$failed = true;
			} else {
				if (mysql_num_rows($return) == 1) {
					$data = mysql_fetch_assoc($return);
					$employeeNumber = $data['Employee_number'];
					$gender = $data['Employee_gender'];
					$_SESSION['employeeNumber'] = $employeeNumber;
					$_SESSION['gender'] = $gender;
				}else if (mysql_num_rows($return) > 1){
					$moreThanOneFlag = true;
				}else{
					$errorFlag = 2;
				}
			}
			
			if ($moreThanOneFlag) {
				//echo "More than one employee found. Try to search by employee number, instead.";
			}else if($employeeNumber != ""){
				header('Location: menu.php');
				$viewLeaveFlag = 1;
			}
			mysql_close($link);
		}
	}
	
	
	if ($viewLeaveFlag != 1){
		echo "
		<html xmlns='http://www.w3.org/1999/xhtml'>
			<head>
				<link href='template.css' rel='stylesheet' type='text/css' media='screen' />
				<title>Search Employee</title>
				<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js' type='text/javascript'></script>
				<script src='jquery.js'></script>
				<script>
				function menu() {
				window.location('menu.php');
				}
				var timeout = 500;
				var closetimer = 0;
				var ddmenuitem = 0;

				function jsddm_open()
				{  jsddm_canceltimer();
				   jsddm_close();
				   ddmenuitem = $(this).find('ul').css('visibility', 'visible');}

				function jsddm_close()
				{  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

				function jsddm_timer()
				{  closetimer = window.setTimeout(jsddm_close, timeout);}

				function jsddm_canceltimer()
				{  if(closetimer)
				   {  window.clearTimeout(closetimer);
					  closetimer = null;}}

				$(document).ready(function()
				{  $('#jsddm > li').bind('mouseover', jsddm_open)
				   $('#jsddm > li').bind('mouseout',  jsddm_timer)});

				document.onclick = jsddm_close;
				
				$(document).ready(function(){					
					$('a').css({
					'text-decoration':'none'
				   });
				   $('a').hover(function () {
						$(this).css({'text-decoration':'none','font-weight':'bold'});
					  }, function () {
						var cssObj = {
						  'text-decoration':'none',
						  'font-weight':'',
						}
						$(this).css(cssObj);
					  });
				   
				   $('#welcome').css({
					'font-size':'30px',
					'border':'5px solid white'
				   });
					
					$('#searchForEmpNum').css({
						'background-color':'white',
						'color':'#2F2825'
					});
					$('#searchForEmpNum').hover(function () {
						$(this).css({'background-color' : '#993333','color':'white','font-weight':'bold'});
					  }, function () {
						var cssObj = {
						  'background-color' : 'white',
						  'color' : '#2F2825',
						  'font-weight':''
						}
						$(this).css(cssObj);
					  });				
					  
					$('#searchForName').css({
						'background-color':'white',
						'color':'#2F2825'
					});
					$('#searchForName').hover(function () {
						$(this).css({'background-color' : '#993333','color':'white','font-weight':'bold'});
					  }, function () {
						var cssObj = {
						  'background-color' : 'white',
						  'color' : '#2F2825',
						  'font-weight':''
						}
						$(this).css(cssObj);
					  });  
				 });
				</script>
			</head>
			<body>
			<!-- start header -->
			<div id='header'>
				<div id='logo'>
					<h1><span>UPLB</span> Leave Application</h1><br/>
					<ul id='jsddm'>";
					if(isset($_SESSION['login']) && $_SESSION['login']==true) {
					echo"
						<li><a href='#'>ACTIONS <span>&#x25BC</span></a>
							<ul>
								<li><a href='AddEmployee.php'>Add Employee</a></li>
								<li><a href='searchEmployee.php'>Change Employee</a></li>
							</ul>
						</li>
						<li><a href='logout.php'>LOG OUT</a>
						</li>";
					}
					echo"
					</ul>
				</div>
			</div>
			<!-- end header -->
			<div id='wrapper'>
				<!-- start page -->
				<div id='page'>
				<div id='page-bg'>
					<div id='sidebar1' class='sidebar'>			
					</div>
					<!-- start content -->
					<div id='content'>";
						
			if($moreThanOneFlag){
				echo "ERROR: More than one employee found. Search by employee number, instead.<br/>";
			}
						echo "<div class='post'>
							<div class='entry'>";
		if (isset($_SESSION['username'])) {
			$username = $_SESSION['username'];			
			echo "
			<div class='greeting' align='center'><p>Welcome, " . $username . "!</p></div><br />
			<form name='viewLeaveReport' method='post' action='searchEmployee.php'>";
			if($errorFlag == 2){
				echo "<span class='error2'>ERROR: Employee not found</span><br/>";
			} 
			echo "<table>
			<tr>
				<td>
					By Employee Number
				</td>
			</tr>
			<tr>
				<td>
					Employee Number: 
				</td>
				<td>
					<input type='text' name='employeeNumber' id='employeeNumber' maxlength='9' value='";if (isset($_POST['employeeNumber'])) {echo $_POST['employeeNumber'];} else {echo "";}  
					echo "'/>
				</td>
				<td>";
					if($errorFlag == 1){
						echo "<span class='error'>Invalid (ex: 123456789)</span>";
					}else{
						echo "&nbsp;";
					}			
			echo "
				</td>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td>
					<input type='submit' class='buttons' name='searchForEmpNum' id='searchForEmpNum' value='Search'/>
				</td>
			</tr>
			</form>";
			echo "
			<tr>
				<td>
					By Employee Name
				</td>
			</tr>
			<form name='searchLeaveName' method='post' action='searchEmployee.php'>
			<tr>
				<td>
					First Name: 
				</td>
				<td>
					<input type='text' name='empFirstName' value='";if (isset($_POST['empFirstName'])) {echo $_POST['empFirstName'];} else echo"";
					echo "'/>
				</td>
				<td>";
					if (!$checkFirstName) echo "<span class='error'>Invalid (ex: Rommel)</span>";
					 else echo "&nbsp;";
						
			echo "
				</td>
			</tr>
			<tr>
				<td>
					Middle Name: 
				</td>
				<td>
					<input type='text' name='empMidName' value='";if (isset($_POST['empMidName'])) {echo $_POST['empMidName'];} else echo"";
					echo"'/>
				</td>
				<td>";
					if (!$checkMidName) echo "<span class='error'>Invalid (ex: Tralala)</span>";
					else echo "&nbsp;";
			echo"
				</td>
			</tr>
			<tr>
				<td>
					Last Name: 
				</td>
				<td>
					<input type='text' name='empLastName' value='";if (isset($_POST['empLastName'])) {echo $_POST['empLastName'];} else echo"";
					echo"'/>
				</td>
				<td>";
					if (!$checkLastName) echo "<span class='error'>Invalid (ex: Bulalacao)</span>";
					else echo "&nbsp;";
			echo "
				</td>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td>
					<input type='submit' class='buttons' name='searchForName' id='searchForName' value='Search' />
				</td>
			</tr>";
			
			echo "
			</form>
			</table>";
		} else {
			echo "You're not supposed to be here!<br/><br/>
			Would you like to <a href='index.php'>Log In</a> or <a href='register.php'>Register</a> ? <br/>
			";
		}
		echo "			</div>
					</div>
				</div>
				<!-- end content -->
				<!-- start sidebars -->
				<div id='sidebar2' class='sidebar'>
				</div>
				<!-- end sidebars -->
				<div style='clear: both;'>&nbsp;</div>
			</div>
			</div>
			<!-- end page -->
		</div>
		<div id='footer'>	
		</div>
		</body>
		</html>";
	}
?>